package com.ljkj.cordial.chat.ui.views;

import com.lanjiu.lib.imclient.bus.EventConstants;
import com.lanjiu.lib.imclient.bus.MessageEvent;
import com.lanjiu.lib.imclient.dto.FriendDto;
import com.lanjiu.lib.imclient.dto.base.Dto;
import com.lanjiu.lib.imclient.user.UserPreferences;
import com.lanjiu.lib.utils.log.Logger;
import com.ljkj.cordial.chat.ui.IView;

/**
 * @description:
 * @author: Panwei
 * @date: 2019/12/12 16:23
 */
public class FriendInfoView extends IView {

    private FriendInfoCallback mCallback;

    public FriendInfoView(FriendInfoCallback mCallback) {
        this.mCallback = mCallback;
    }

    public void doGetFriendInfo(String friendUserId) {
        int action = UserPreferences.isRegistered() ? EventConstants.Action.FRIEND_INFO_REGISTERED : EventConstants.Action.FRIEND_INFO_TOURISTS;
        doSendRequest(action, friendUserId);
    }

    public void doSetFriendOfflineMsg(String friendUserId, String friendUserType, boolean isOfflineMsg) {
        FriendDto friendDto = new FriendDto(UserPreferences.getUserType());
        friendDto.setFriendUserId(friendUserId);
        friendDto.setUserType(friendUserType);
        friendDto.setOfflineMessage(isOfflineMsg);
        int action = UserPreferences.isRegistered() ? EventConstants.Action.FRIEND_OFFLINE_MSG_REGISTERED : EventConstants.Action.FRIEND_OFFLINE_MSG_TOURISTS;
        doSendRequest(action, friendDto.toJson());
    }

    public void doDeleteFriend(String friendUserId, String friendUserType) {
        FriendDto friendDto = new FriendDto(UserPreferences.getUserType());
        friendDto.setFriendUserId(friendUserId);
        friendDto.setUserType(friendUserType);
        int action = UserPreferences.isRegistered() ? EventConstants.Action.FRIEND_DELETE_REGISTERED : EventConstants.Action.FRIEND_DELETE_TOURISTS;
        doSendRequest(action, friendDto.toJson());
    }

    @Override
    protected void onMessageEventHandle(MessageEvent event) {
        Logger.print("FriendInfoView", event.toString());
        if (EventConstants.Action.FRIEND_OFFLINE_MSG_TOURISTS == event.action() ||
                EventConstants.Action.FRIEND_OFFLINE_MSG_REGISTERED == event.action()) {
            FriendDto friend = null;
            if (checkIsSuccess(event.code(), event.data()))
                friend = Dto.fromJson(event.data(), FriendDto.class);
            if (null != mCallback)
                mCallback.onSetFriendOfflineMsgCallback(event.code(), event.msg(), friend);
        } else if (EventConstants.Action.FRIEND_DELETE_TOURISTS == event.action() ||
                EventConstants.Action.FRIEND_DELETE_REGISTERED == event.action()) {
            if (null != mCallback)
                mCallback.onDeleteFriendCallback(event.code(), event.msg());
        } else if(EventConstants.Action.FRIEND_FIND_TOURISTS == event.action() ||
                EventConstants.Action.FRIEND_FIND_REGISTERED == event.action()) {
            FriendDto data = null;
            if (checkIsSuccess(event.code(), event.data()))
                data = Dto.fromJson(event.data(), FriendDto.class);
            if (null != mCallback)
                mCallback.onFriendInfoCallback(event.code(), event.msg(), data);
        }
    }

    public interface FriendInfoCallback {

        void onSetFriendOfflineMsgCallback(int code, String msg, FriendDto data);

        void onDeleteFriendCallback(int code, String msg);

        void onFriendInfoCallback(int code, String msg, FriendDto data);
    }
}
